﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Timers;
using System.Globalization;
using POSDisplay.PetroFexPOSServiceReference;

namespace POSDisplay
{
    public sealed class PumpInfo
    {
        public string ID { get; set; }
        public string Litres { get; set; }
        public string Sale { get; set; }
        public string Fuel { get; set; }
        public string Ready { get; set; }
        public string Active { get; set; }
    }

     ///<summary>
     ///Interaction logic for MainWindow.xaml
     ///</summary>
     
    public partial class MainWindow : Window
    {
        System.Timers.Timer m_PumpUpdateTimer = new System.Timers.Timer(250);
        System.Timers.Timer m_ButtonUpdateTimer = new System.Timers.Timer(250);
        PetroFexServiceClient client = new PetroFexServiceClient();
        List<PetrolPump> pumps;

        private string m_POSId = "";

        public MainWindow()
        {
            InitializeComponent();
            pumps = client.getPumps();
            int inactivePOSId = client.GetInactivePOSId();
            m_POSId = inactivePOSId.ToString();
            PopulateListView();
            client.ConnectPOS(m_POSId);
            this.Title = "PetroFex POS - ID: " + m_POSId;
            var pumpUpdater = new ThreadStart(updatePumpStates);
            pumpUpdater.BeginInvoke(null, null);
        }

        private void PopulateListView()
        {
            foreach (PetrolPump p in pumps)
            {
                PumpInfo info = new PumpInfo
                {
                    ID = p.PumpId,
                    Litres = p.LitresPumped.ToString("N", new CultureInfo("en-GB")),
                    Sale = p.ThisSale.ToString("N", new CultureInfo("en-GB")),
                    Fuel = p.CurrentFuelType.ToString(),
                    Ready = p.CustomerReady.ToString(),
                    Active = p.PumpActive.ToString()
                };
                pumpList.Items.Add(info);
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            client.DisconnectPOS(m_POSId);
        }

        private void authoriseBtn_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = pumpList.SelectedIndex;
            if (selectedIndex != -1)
            {
                PumpInfo pi = (PumpInfo)pumpList.Items.GetItemAt(pumpList.SelectedIndex);
                if (bool.Parse(pi.Ready) && !bool.Parse(pi.Active))
                    client.ActivatePump(pi.ID);
                else
                    MessageBox.Show("Pump not ready or is already active!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
                MessageBox.Show("No pumps selected", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void updatePumpStates()
        {
            m_PumpUpdateTimer.Elapsed += (sender, args) =>
            {
                pumps = client.getPumps();
                int selectedPump = 0;
                Application.Current.Dispatcher.BeginInvoke(new Action(() => selectedPump = pumpList.SelectedIndex));
                Application.Current.Dispatcher.BeginInvoke(new Action(() => pumpList.Items.Clear()));
                Application.Current.Dispatcher.BeginInvoke(new Action(() => PopulateListView()));
                Application.Current.Dispatcher.BeginInvoke(new Action(() => pumpList.SelectedIndex = selectedPump));
            };
            m_PumpUpdateTimer.Start();
        }
    }
}
